<?php
namespace mini;
//路由类的简单实现
//模块 =》控制器=》方法 =》参数
//层次  http://localhost/home/index/test/id/3 pathinfo模式
//url 传参模式  http://localhost/index.php?m=home&c=index&a=test&id=3

class Router{

    static private $url_mode;  //url解析模式
    static private $var_controller;
    static private $var_action;
    static private $var_module;
    
    /**
     * 路由器初始化方法
     * @Author   Jonas.Yang
     * @DateTime 2017-03-18T23:14:22+0800
     * @return   [type]                   [description]
     */
    static function init(){
    	self::$url_mode = URL_MODE;
    	//self::$var_controller = $config['VAR_CONTROLLER'];
    	//self::$var_action = $config['VAR_ACTION'];
    	//self::$var_module = $config['VAR_MODULE']; 
    }
    
    /**
     * 解析Url参数
     * @Author   Jonas.Yang
     * @DateTime 2017-03-25T15:01:21+0800
     * @return   [type]                   [description]
     */
    static function run(){
        self::init();
        switch (self::$url_mode) {
             //动态url传参 模式
            case 0:
                return self::getParamByDynamic();
            //pathinfo 模式
            case 1:
                return self::getParamByPathinfo();
        }
    }
    
    /**
     * 获取参数，通过url传参模式
     * @Author   Jonas.Yang
     * @DateTime 2017-03-18T23:16:05+0800
     * @return   [type]                   [description]
     */
    private static function getParamByDynamic(){
        $arr = empty($_SERVER['QUERY_STRING']) ? array() : explode("&", $_SERVER['QUERY_STRING']);
        $data = array(
            'module' => '',
            'controller' => '',
            'action' => '',
            'param' => array()
        );
        if(!empty($arr)){
            $tmp = array();
            $part = array();
            foreach ($arr as $value) {
                $tmp = explode("=", $value);
                $tmp[1] = isset($tmp[1]) ? trim($tmp[1]) : '';
                $part[$tmp[0]] = $tmp[1];
            }
            //解析出模块-控制器-方法
            if(isset($part[self::$var_module])){
                $data['module'] = $part[self::$var_module];
                unset($part[self::$var_module]);
            }
            if(isset($part[self::$var_controller])){
                $data['controller'] = $part[self::$var_controller];
                unset($part[self::$var_controller]);
            }
            if(isset($part[self::$var_action])){
                $data['action'] = $part[self::$var_action];
                unset($part[self::$var_action]);
            }
            switch ($_SERVER['REQUEST_METHOD']) {
                case 'GET':
                    unset($_GET[self::$var_controller], $_GET[self::$var_action], $_GET[self::$var_module]);
                    $data['param'] = array_merge($part, $_GET);
                    unset($_GET);
                    break;
                case 'POST':
                    unset($_POST[self::$var_controller], $_POST[self::$var_action], $_GET[self::$var_module]);
                    $data['param'] = array_merge($part, $_POST);
                    unset($_POST);
                    break;
                case 'HEAD':
                    break;
                case 'PUT':
                    break;
            }

        }
        return $data;
    }

    /**
     * 通过Pathinfo模式获取参数
     * @Author   Jonas.Yang
     * @DateTime 2017-03-25T14:29:54+0800
     * @return   [type]                   [description]
     */
    private static function getParamByPathinfo(){
        $part = empty($_SERVER['QUERY_STRING']) ? array() : explode("/", $_SERVER['QUERY_STRING']);
        $data = array(
            'module' => '',
            'controller' => '',
            'action' => '',
            'param' => array()
        );
        if(!empty($part)){
            krsort($part);  //按照键名倒序,参数全部压到栈底
            $data['module'] = array_pop($part); //出栈，出栈顶 0，1，2
            $data["controller"] = array_pop($part);
            $data["action"] = array_pop($part);
            ksort($part);
            $part = array_values($part);
            $temp = array();
            if(!empty(tmp)){
                foreach($part as $key => $v){
                    if($key % 2 == 0){
                        $temp[$v] = isset($part[$key+1]) ? $part[$key+1]: '';
                    }
                }
            }
            switch ($_SERVER['REQUEST_METHOD']) {
                case 'GET':
                    $data['param'] = $temp;
                    $_GET = $data['param'];
                    break;
                
                 case 'POST':
                    unset($_POST[self::$var_controller], $_POST[self::$var_action], $_POST[self::$var_module]);
                    $data['param'] = array_merge($temp, $_POST);
                    $_POST = $data['param'];
                    break;

                case 'HEAD':
                    break;
                case 'PUT':
                    break;
            }

        }
        return $data;
    }

}